Fix #3475: Emit 'true ? null : new { ... }' for null of anonymous type#3866
Open
siegfriedpammer wants to merge 1 commit into
Open
Fix #3475: Emit 'true ? null : new { ... }' for null of anonymous type#3866siegfriedpammer wants to merge 1 commit into
siegfriedpammer wants to merge 1 commit into
Conversation
cb87553 to
4345bfe
Compare
When a null literal is the only argument a generic type argument could be inferred from, and that type argument is an anonymous type, ILSpy used to drop the type arguments entirely (they cannot be written explicitly), producing 'Test(null)', which no longer compiles (CS0411). A conditional expression whose never-taken branch creates an instance of the anonymous type is the minimal C# expression that gives a null value that type, so the argument now carries enough information for type inference. The instance expression is obtained by translating a synthesized 'newobj' with 'default.value' arguments through the existing pipeline rather than assembling syntax by hand; the property values are typed defaults, since the IL only contains ldnull. Anonymous types occurring inside other constructed types (e.g. arrays) stay unexpressible and keep the previous output. Assisted-by: Claude:claude-fable-5:Claude Code
4345bfe to
38203f8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3475.
Problem:
Test(true ? null : new { A = 1, B = 2 })compiles to a bareldnullpassed toTest<<>f__AnonymousType0<int, int>>. The type argument cannot be written explicitly (anonymous types cannot be named), so ILSpy silently dropped the type argument list and emittedTest(null), which no longer compiles (CS0411).Approach: when type inference fails and the type arguments contain anonymous types,
CallBuildernow rewrites null-literal arguments of anonymous type astrue ? null : new { A = default(int), B = default(int) }-- a conditional expression whose never-taken branch creates an instance of the anonymous type. This is the minimal C# expression that produces a null value of an anonymous type, and it makes the type arguments inferable again. Roslyn folds the constant conditional back toldnull, so the output round-trips.The instance expression is not assembled by hand: a synthesized
newobjinstruction withdefault.valuearguments (nestednewobjfor directly nested anonymous types) is run through the existing translation pipeline, which already renders anonymous-type creations as object-initializer syntax with exactly-typed values (default(int),(string)null, ...). Exact types matter because anonymous-type property types are inferred from the initializer values. The original property values are not recoverable (the IL contains onlyldnull) and are never evaluated.Anonymous types that occur inside other constructed types (e.g.
List<T>or arrays of an anonymous type) cannot be expressed without naming the type; those keep the previous output.Verification: new round-trip cases in the
AnonymousTypespretty test, covering the plain, string-property, and nested cases (green in all 12 configurations runnable on Linux; mcs/legacy-csc configurations need Windows CI); full decompiler suite passes; the issue's exact reproduction decompiles to compilable code that prints the same anonymous type name at runtime as the original assembly.🤖 Generated with Claude Code